<?php

class Http
{
    public static function sendByAsync($url, $send_str = '', $header_arr = [])
    {

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $send_str,
            CURLOPT_HTTPHEADER => $header_arr,
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            echo $response;
        }
        die;
        $url_array = parse_url($url);
        $port = isset($url_array['port']) ? $url_array['port'] : 80;
        $fp = fsockopen($url_array['host'], $port, $err_num, $err_str, 30);
        if (!$fp) {
            return FALSE;
        }

        if (isset($url_array['query'])) {
            $getPath = $url_array['path'] . "?" . $url_array['query'];
        } else {
            $getPath = $url_array['path'];
        }
        $getPath = rtrim($getPath, '?');
        $method = !empty($send_str) ? 'POST' : 'GET';
        $header = $method . " " . $getPath . " " . "HTTP/1.1\r\n";
        $header .= "Host:" . $url_array['host'] . "\r\n";
        if (!empty($header_arr)) {
            foreach ($header_arr as $k => $v) {
                $header .= $v . " \r\n";
            }
        }
        //以raw(原数据)形式传数据
        if (!empty($send_str)) {

            $send_str = !is_string($send_str) ? json_encode($send_str) : $send_str;
            $raw_str = "";
            $raw_str .= "Content-Length:" . strlen($send_str) . " \r\n";
            $raw_str .= "Connection:close\r\n\r\n";
            $raw_str .= $send_str . "\r\n"; //传递数据
        }
        if (!empty($raw_str)) {
            $header .= $raw_str;
        } else {
            $header .= "Connection:close\r\n\r\n";;
        }

        //  echo($header);
        fwrite($fp, $header);
        $result = '';
        while (!feof($fp)) {//测试文件指针是否到了文件结束的位置
            $result .= fread($fp, 1024);
        }
        fclose($fp);
        return $result;
    }

    public static function request($url, $content, $file_name, $md5 = '')
    {
        //echo '===========================';
        //echo "\n开始：" . $file_name . "\n";
        $header_arr = [
            'binfile-md5:  ' . $md5,
//            'binfile-auth: syjcx',
            'binfile-auth: 13YehO6WQOVHN3fY',
            'binfile-gzip: false',
            'Content-Type: multipart/form-data; boundary=syjcxguangdong'
        ];

        $sendStr = "--syjcxguangdong
Content-Disposition: form-data; name=\"binFile\"; filename=\"$file_name\"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary\r\n
$content
--syjcxguangdong
Content-Disposition: form-data; name=\"filename\"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit\r\n
$file_name\r\n
--syjcxguangdong--";

        self::sendByAsync($url, $sendStr, $header_arr);
    }
}

define('APP_ROOT', __DIR__);
$dir = APP_ROOT . "/tmp";
$config = include APP_ROOT . '/../config/config.php';
$token = false;
$handle = opendir($dir);

while (false !== $file = (readdir($handle))) {
    if ($file != '.' && $file != '..') {
        // 文件路径
        $files = realpath($dir . '/' . $file);
        // 文件名称
        $file_name = basename($files);
        // 文件内容
        $content = file_get_contents($files);
        // 生成MD5序列，必须
        $md5 = md5_file($files);
        if (in_array($file, ['syjcx_BASIC_CLJB_REQ_20180717143546316.json'])) {
            $token = true;
            Http::request($config['api_gd_url'], $content, $file_name, $md5);
        }

        if (file_exists($files)) {
//            Http::request($config['api_gd_url'], $content, $file_name, $md5);
        }
    }
}
closedir($handle);